package com.GatherFour;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class GatherFourActivity extends Activity {
    /** Called when the activity is first created. */
	private Button createHost;
	private Button scanHost;
	private boolean isWifiP2pEnabled = false;
	private BroadcastReceiver receiver = null;
	private final IntentFilter intentFilter = new IntentFilter();
	
    public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) {
        this.isWifiP2pEnabled = isWifiP2pEnabled;
        if(isWifiP2pEnabled == false){
    	Toast.makeText(GatherFourActivity.this,"WiFi-Direct is disabled, please enable it in system settings.",
                Toast.LENGTH_LONG).show();
        }else{
        	Toast.makeText(GatherFourActivity.this,"WiFi-Direct Available.",
                    Toast.LENGTH_SHORT).show();
        }
        
    }
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        
        setButtonView();
        
    }
    
    /** Initialize and link buttons to respective IDs. */
    public void setButtonView(){
    	createHost = (Button)this.findViewById(R.id.createHostID);
    	scanHost = (Button)this.findViewById(R.id.scanHostID);
    	
    	
    	
    	
    	createHost.setOnClickListener(new View.OnClickListener() {
		
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//if(isWifiP2pEnabled == true){
				//Intent myIntent = new Intent(v.getContext(), WiFiDirectHost.class);
				Intent myIntent = new Intent(v.getContext(),GameLobby.class);
				Bundle bundle = new Bundle();
				//bundle.putString("hostAddr", hostAddr);
				bundle.putString("self", "host");
				myIntent.putExtras(bundle);
				startActivity(myIntent);
				/*} else{
					Toast.makeText(GatherFourActivity.this,"WiFi-Direct is disabled, please enable it in system settings.",
			                Toast.LENGTH_LONG).show();
				}*/
				
			}   		
    	});
    	
    	scanHost.setOnClickListener(new View.OnClickListener() {
    		
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//if(isWifiP2pEnabled == true){
				//Intent myIntent = new Intent(v.getContext(), WiFiDirectScanner.class);
				Intent myIntent = new Intent(v.getContext(),GameLobby.class);
				Bundle bundle = new Bundle();
				//bundle.putString("hostAddr", hostAddr);
				bundle.putString("self", "client");
				myIntent.putExtras(bundle);
				startActivity(myIntent);
				/*}else{
					Toast.makeText(GatherFourActivity.this,"WiFi-Direct is disabled, please enable it in system settings.",
			                Toast.LENGTH_LONG).show();
				}*/
			}   		
    	});
    }
    
    @Override
    public void onResume() {
        super.onResume();
        receiver = new WiFiDirectBroadcastReceiver(null, null, this);
        registerReceiver(receiver, intentFilter);
    }

    @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }
    
}